using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lync.Widget.Library.Presentation.Entities;
using Lync.Widget.Library.Presentation.Models;
using Lync.Widget.Library.Presentation.Presenters;

namespace Lync.Widget.Library.Presentation.Views
{
    public class GroupMembersView : UserControl, IGroupMembersView
    {
        #region Controls

        protected Repeater MembersOfGroupRepeater;
        protected Button SaveButton;
        protected Panel MembersPanel;

        #endregion

        #region View Properties

        public bool ShowSaveButton
        {
            set { SaveButton.Visible = value; }
        }


        public IGroupMembersPresenter Presenter { get; private set; }

        public IList<IGroupMember> SelectedMembers
        {
            get
            {
                var temp = new List<IGroupMember>();
                FillSelectedMembers(temp);
                return temp;
            }
        }

        public string GroupName { get; set; }
        public string OrganisationUnit { get; set; }

        private IList<IGroupMember> m_Members;
        public IList<IGroupMember> Members
        {
            get { return m_Members; }
            set
            {
                m_Members = value;
                MembersOfGroupRepeater.DataSource = m_Members;
                MembersOfGroupRepeater.DataBind();
            }
        }

        #endregion


        protected override void OnInit(System.EventArgs e)
        {
            Presenter = new GroupMembersPresenter(this, new GroupsModel());
        }

        protected override void OnLoad(System.EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulatePriorities();
            }            
        }

        private void FillSelectedMembers(List<IGroupMember> members)
        {
            var items = (from item in MembersOfGroupRepeater.Items.Cast<RepeaterItem>().ToList()
                         where (((CheckBox)item.FindControl("GroupMembersCheckBox")).Checked)
                         select new GroupMember
                         {
                             Sip = ((Label)item.FindControl("SipLabel")).Text,
                             Priority = Convert.ToInt32(((DropDownList)item.FindControl("PriorityDropDownList")).SelectedValue),
                         });

            members.AddRange(items);
        }

        protected void SelectAllCheckBox_OnChanged(object sender, EventArgs e)
        {
            var checkbox = (CheckBox)sender;
            MembersOfGroupRepeater.Items.Cast<RepeaterItem>().ToList().ForEach(item => ((CheckBox)item.FindControl("GroupMembersCheckBox")).Checked = checkbox.Checked);
        }

        private void PopulatePriorities()
        {
            if (m_Members != null)
            {
                var i = 1;
                foreach (var item in MembersOfGroupRepeater.Items.Cast<RepeaterItem>().ToList())
                {
                    var member = m_Members.ToArray()[i-1];

                    if (member.Priority == 0)
                    {
                        member.Priority = m_Members.Max(m => m.Priority) + 1;
                    }

                    ((DropDownList)item.FindControl("PriorityDropDownList")).SelectedValue =
                        member.Priority.ToString(CultureInfo.InvariantCulture);
                    i++;
                }
            }
        }
    }
}